package com.swpu.service_knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_knowledge.entity.ArticleCollect;
import com.swpu.service_knowledge.mapper.ArticleCollectMapper;
import com.swpu.service_knowledge.service.ArticleCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-27
 */
@Service
public class ArticleCollectServiceImpl extends ServiceImpl<ArticleCollectMapper, ArticleCollect> implements ArticleCollectService {

    @Override
    public void addArticleCollect(String openid, Integer articleId) {
        LambdaQueryWrapper<ArticleCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleCollect::getArticleId,articleId).eq(ArticleCollect::getUserId,openid);
        ArticleCollect articleCollect = this.getOne(queryWrapper);
        if (articleCollect==null){
            ArticleCollect collect = new ArticleCollect();
            collect.setArticleId(articleId);
            collect.setUserId(openid);
            this.save(collect);
        }

    }

    @Override
    public void deleteArticleCollect(String openid, Integer articleId) {
        LambdaQueryWrapper<ArticleCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleCollect::getArticleId,articleId).eq(ArticleCollect::getUserId,openid);
        boolean remove = this.remove(queryWrapper);
        if (!remove) throw new RuntimeException("删除收藏失败");
    }

    @Override
    public int getArticleCollectStatus(String openid, Integer articleId) {
        LambdaQueryWrapper<ArticleCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleCollect::getArticleId,articleId).eq(ArticleCollect::getUserId,openid);
        int count = this.count(queryWrapper);
        return count;
    }
}
